﻿using AutoMapper;
using NFine.Data.Dto.WeixinManage;
using NFine.Data.Entity.WeixinManage;
using NFine.Repository.WeixinManage;
using NFine.Util;
using SharpRepository.Repository.Queries;
using SharpRepository.Repository.Specifications;
using System;
using System.Collections.Generic;
using System.Linq;

namespace NFine.Service.WeixinManage
{
    public class WxUserService:WxMpCommonService
    {
        WxAccountRepository wxAccountRepository = new WxAccountRepository(SharpRepoConfig.sharpRepoConfig, "efCore");
        WxUserRepository wxUserRepository = new WxUserRepository(SharpRepoConfig.sharpRepoConfig, "efCore");

        public List<WxUserGridDto> GetList(Pagination pagination, string keyword)
        {
            string appId = OperatorProvider.Provider.GetOperator().WxAccountModel.AppId;
            var specification = new Specification<WxUser>(p => p.AppId == appId);
            var pagingOptions = new PagingOptions<WxUser, DateTime?>(pagination.page, pagination.rows, x => x.SubscribeTime, isDescending: true);
            if (!string.IsNullOrEmpty(keyword))
            {
                specification = new Specification<WxUser>(p => p.AppId == appId && (p.Nickname.Contains(keyword)));
            }
            var list = wxUserRepository.FindAll(specification, pagingOptions).ToList();
            pagination.records = pagingOptions.TotalItems;
            return Mapper.Map<List<WxUserGridDto>>(list);
        }

        public void SyncUserInfo()
        {
            var specification = new Specification<WxUser>(p => p.AppId == AccountModel.AppId && p.SubscribeStatus == 1 && p.SynchronisedTime == null);
            List<WxUser> wxUserList = wxUserRepository.FindAll(specification).ToList();
            List<string> openIdsList = new List<string>();
            for (var i = 0; i < wxUserList.Count(); i++)
            {
                openIdsList.Add(wxUserList[i].OpenId);
            }
            string appId = OperatorProvider.Provider.GetOperator().WxAccountModel.AppId;
            string AppSecret = OperatorProvider.Provider.GetOperator().WxAccountModel.AppSecret;

            string accessToken = CommonService.GetAccessToken().AccessToken;
            Rabbit.WeiXin.MP.Api.User.IUserService userService = new Rabbit.WeiXin.MP.Api.User.UserService(base.AccountModel);
            var aaa = Rabbit.WeiXin.MP.Api.User.UserServiceExtensions.GetUserInfoList(userService, openIdsList.ToArray());
        }
        //public void SyncUserInfo()
        //{
        //    string appId = OperatorProvider.Provider.GetOperator().WxAccountModel.AppId;
        //    string appSecret = OperatorProvider.Provider.GetOperator().WxAccountModel.AppSecret;
        //    AccessTokenResult accessTokenResult = AccessTokenContainer.GetAccessTokenResult(appId);
        //    var specification = new Specification<WxUser>(p => p.AppId == appId && p.SubscribeStatus == 1);
        //    List<BatchGetUserInfoData> batchGetUserInfoDataList = new List<BatchGetUserInfoData>();
        //    List<WxUser> wxUserList = wxUserRepository.FindAll(specification).ToList();
        //    if (wxUserList.Count > 0)
        //    {
        //        foreach (WxUser wxuser in wxUserList)
        //        {
        //            int index = wxUserList.IndexOf(wxuser);
        //            if (index > 100)
        //            {
        //                break;
        //            }
        //            BatchGetUserInfoData batchGetUserInfoData = new BatchGetUserInfoData();
        //            batchGetUserInfoData.openid = wxuser.OpenId;
        //            batchGetUserInfoData.lang = "zh_CN";
        //            batchGetUserInfoData.LangEnum = 0;
        //            batchGetUserInfoDataList.Add(batchGetUserInfoData);
        //        }
        //        BatchGetUserInfoJsonResult batchGetUserInfoJsonResult =UserApi.BatchGetUserInfo(accessTokenResult.access_token, batchGetUserInfoDataList);
        //        if (batchGetUserInfoJsonResult.ErrorCodeValue == 0)
        //        {
        //            if (batchGetUserInfoJsonResult.user_info_list.Count > 0)
        //            {
        //                foreach (UserInfoJson userInfoJson in batchGetUserInfoJsonResult.user_info_list)
        //                {
        //                    var specification2 = new Specification<WxUser>(p => p.AppId == appId && p.OpenId == userInfoJson.openid);
        //                    WxUser wxUser = wxUserRepository.Find(specification2);
        //                    wxUser.Nickname = userInfoJson.nickname;
        //                    wxUser.Sex = userInfoJson.sex;
        //                    wxUser.HeadImgUrl = userInfoJson.headimgurl;
        //                    wxUser.Country = userInfoJson.country;
        //                    wxUser.Province = userInfoJson.province;
        //                    wxUser.City = userInfoJson.city;
        //                    wxUser.Language = userInfoJson.language;
        //                    wxUser.UnionId = userInfoJson.unionid;
        //                    wxUser.Remark = userInfoJson.remark;
        //                    wxUser.GroupId = userInfoJson.groupid;
        //                    //wxUser.SynchronisedTime = System.DateTime.Now;
        //                    wxUserRepository.Update(wxUser);
        //                }
        //            }
        //        }
        //    }            
        //}
    }
}
